<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>生态瞬时测试</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="../css/paging.css">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>被试者管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-5 m-b-xs">
                            <!--<select class="input-sm form-control input-s-sm inline">-->
                                <!--<option value="0">请选择实验</option>-->
                                <!--<option value="1">选项1</option>-->
                                <!--<option value="2">选项2</option>-->
                                <!--<option value="3">选项3</option>-->
                            <!--</select>-->
                        </div>
                        <div class="col-sm-4 m-b-xs">
                            <!--<div data-toggle="buttons" class="btn-group">-->
                            <!--<label class="btn btn-sm btn-white">-->
                            <!--<input type="radio" id="option1" name="options">天</label>-->
                            <!--<label class="btn btn-sm btn-white active">-->
                            <!--<input type="radio" id="option2" name="options">周</label>-->
                            <!--<label class="btn btn-sm btn-white">-->
                            <!--<input type="radio" id="option3" name="options">月</label>-->
                            <!--</div>-->
                        </div>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <input type="text" class="input-sm form-control" id="search-box" placeholder="输入用户名或真实姓名"> <span class="input-group-btn">
                                        <button type="button" class="btn btn-sm btn-primary" id="search-btn">搜索</button> </span>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                            <tr>

                                <th></th>
                                <th>用户名</th>
                                <th>真实姓名</th>
                                <th>联系方式</th>
                                <th>年龄</th>
                                <th>性别</th>
                                <th>出生日期</th>
                                <th class="text-center" >操作</th>
                            </tr>
                            </thead>
                            <tbody id="user-box">

                            </tbody>
                        </table>
                    </div>

                    <div class="paging">
                        <div id="paging" class="page_div"></div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>
<!--paging-->
<script src="../js/paging.js"></script>
<script src="../js/dateChange.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });
</script>
<script>
    $(document).ready(function () {
        //获取页面数据
        function initData(_data) {
            var token = sessionStorage.getItem("token");
            var num = _data.current;
            var pageSize = _data.size;
            $.ajax({
                type:"GET",
                url:"http://123.206.18.189:8080/users",
                data:_data,
                beforeSend: function(xhr){xhr.setRequestHeader('authToken', token);},
                dataType:'json',
                success: function (res) {
                    var html = '';
                    for(var i=0; i<res.data.length;i++){
                        var offset = pageSize *(num - 1) + 1;
                       var day = dateChange(res.data[i].birthday);
                        html += ` <tr alt="${res.data[i].id}">
                                <td>${i + offset}</td>
                                <td>${res.data[i].userName}</td>
                                <td>${res.data[i].realName}</td>
                                <td>${res.data[i].telephone}</td>
                                <td>${res.data[i].age}</td>
                                <td>${res.data[i].gender}</td>
                                <td>${day}</td>
                                <td>
                                    <i class="fa fa-edit edit" title="修改" style="margin-right: 20px;"></i>
                                    <i class="fa fa-eye getDetails" title="查看" style="margin-right: 20px;"></i>
                                    <i class="fa fa-trash-o del" title="删除" style="margin-right: 20px;"></i>

                                </td>
                            </tr>`
                    }
                    $('#user-box').html(html);
                    updatePage(res,_data);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(XMLHttpRequest.readyState);
                    console.log(textStatus);
                },

            });
        }
        initData({role:"SUBJECT",current:1,size:10});
        //分页效果
        function updatePage(res,_data) {
            $("#paging").paging({
                pageNo:_data.current,
                totalPage:Math.ceil(res.meta.pagination.total/res.meta.pagination.size),
                callback: function (num) {
                    _data.current = num;
                    initData(_data)
                }
            })
        }

        //删除用户
        $("#user-box").on("click",".del",function () {
            var token = sessionStorage.getItem("token");
            var userId = $(this).parent().parent().attr("alt");
            console.log(userId);
            var r = confirm("您是否确定要删除该用户？");
            if(r==true){
                $.ajax({
                    type:"DELETE",
                    url:"http://123.206.18.189:8080/users/"+userId,
                    beforeSend: function(xhr){xhr.setRequestHeader('authToken', token);},
                    dataType:'json',
                    complete:function () {
                        window.location.reload();
                    }
                })
            }

        })

        //查看当用户信息
        $("#user-box").on("click",".getDetails",function () {
            var userId = $(this).parent().parent().attr("alt");
            console.log(userId);
            localStorage.setItem("userID",userId);
            window.location.href = "userInfo.html";

        })
        //模糊查询
        $("#search-btn").on("click",function () {
            var key = $("#search-box").val();
            console.log(key);
            // $.ajax({})
            initData({role:"SUBJECT",current:1,size:10,name:key})

        })
        //修改用户信息
        $("#user-box").on("click",".edit",function () {
            var userId = $(this).parent().parent().attr("alt");
            localStorage.setItem("userID",userId);
            window.location.href = "userInfoChange.html";
        })


    })
</script>
</body>

</html>
